package com.hwadee.springboot.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwadee.springboot.entity.Exam;
import com.hwadee.springboot.entity.Question;
import com.hwadee.springboot.entity.StudentExam;
import com.hwadee.springboot.mapper.QuestionMapper;
import com.hwadee.springboot.mapper.StudentExamMapper;
import com.hwadee.springboot.service.ExamService;
import com.hwadee.springboot.mapper.ExamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 86152
* @description 针对表【exam】的数据库操作Service实现
* @createDate 2025-05-21 09:23:00
*/
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private StudentExamMapper studentExamMapper;
    @Override
    public List<Exam> getAvailableExams(Long studentId) {
        // 获取所有考试
        List<Exam> allExams = examMapper.selectList(null);
        // 获取学生已经参加过的考试ID
        List<StudentExam> studentExams = studentExamMapper.selectList(
                Wrappers.<StudentExam>query().eq("StudentID", studentId)
        );


        Set<Integer> takenExamIds = studentExams.stream()
                .map(StudentExam::getExamid)
                .collect(Collectors.toSet());


        // 过滤掉已经参加过的考试
        List<Exam> availableExams = allExams.stream()
                .filter(exam -> !takenExamIds.contains(exam.getId()))
                .collect(Collectors.toList());


        return availableExams;
    }

    @Override
    public Boolean publishExam(Exam exam) {
        // 保存考试信息到数据库并返回成功或失败的结果
        int insert = examMapper.insert(exam);
        return insert > 0;
    }

    @Override
    public List<Question> listQuestionsByExamId(Integer id) {
        return null; // 实现获取考试题目列表的逻辑
    }
}




